package com.ars.common.dao.UserInfo;

import com.ars.common.beans.User.User;
import com.ars.common.utils.DBUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * ClassName: FindUserById
 * Package: com.ars.common.dao.UserInfo
 * Description:
 *
 * @Author: Bryan Long
 * @Create: 2024/8/29 - 14:37
 * @Version: v1.0
 */
public class FindUserById {
    public static User findUserById(String user_id) throws SQLException {
        User user = new User();

        String sql = "-- 左连接查询\n" +
                "SELECT u.userID, u.mailbox, u.name, u.phone, s.sex, r.role, a.AdminGrade, a.id AS AdminID, u.password\n" +
                "FROM user u\n" +
                "LEFT JOIN role r ON u.role = r.id\n" +
                "LEFT JOIN sex s ON u.sex = s.id\n" +
                "LEFT JOIN admin a ON u.isAdmin = a.id\n" +
                "WHERE u.userID = ?;\n";

        PreparedStatement ps = DBUtil.getConnection().prepareStatement(sql);
        ps.setString(1, user_id);
        ResultSet rs = ps.executeQuery();
        while(rs.next()) {
            user.setUserID(rs.getString("userID"));
            user.setPassword(rs.getString("password"));
            user.setMailbox(rs.getString("mailbox"));
            user.setName(rs.getString("name"));
            user.setPhone(rs.getString("phone"));
            user.setSex(rs.getString("sex"));
            user.setRole(rs.getString("role"));
            user.setAdminID(rs.getString("AdminID"));//管理员等级编号
            user.setAdminName(rs.getString("AdminGrade"));//管理员等级
        }
        return user;
    }
}
